﻿using AEFunction;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace CommonFunction
{

    public class Function
    {
        /// <summary>
        /// 得到ListBox已选择项的集合
        /// </summary>
        /// <param name="listBox1"></param>
        /// <returns></returns>
        public static List<object> GetSelectItems(ListBox listBox1)
        {
            List<object> selectedItems = new List<object>();
            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            {
                selectedItems.Add(listBox1.SelectedItems[i]);
            }
            return selectedItems;
        }

        /// <summary>
        /// 将选择性删除
        /// </summary>
        /// <param name="listBox1"></param>
        /// <param name="selectedItems"></param>
        public static void RemoveSelectItems(ListBox listBox1, List<object> selectedItems)
        {
            for (int i = 0; i < selectedItems.Count; i++)
            {
                listBox1.Items.Remove(selectedItems[i]);
            }
        }

        /// <summary>
        /// 得到Excel的所有字段
        /// </summary>
        public static string[] GetExcelField(DataTable myDataTable)
        {
            List<string> inPutFileds = new List<string>();
            for (int i = 0; i < myDataTable.Columns.Count; i++)
            {
                string str = myDataTable.Rows[0][i].ToString().Trim();
                myDataTable.Columns[i].ColumnName = str;
                inPutFileds.Add(str);
            }
            return inPutFileds.ToArray();
        }

        /// <summary>
        /// 获得字段在DataTable中的列索引
        /// </summary>
        /// <param name="myDataTable"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int GetFieldIndex(DataTable myDataTable, string str)
        {
            if (myDataTable.Columns.Contains(str))
                return myDataTable.Columns.IndexOf(str);
            return -1;
        }

        /// <summary>
        /// 获得字段在DataTable中的列索引集合
        /// </summary>
        /// <param name="myDataTable"></param>
        /// <param name="ExcelSelectedFields"></param>
        /// <returns></returns>
        public static List<int> GetFieldIndexFromTable(DataTable myDataTable, List<ExcelField> ExcelSelectedFields)
        {
            List<int> IndexList = new List<int>();
            foreach (ExcelField str in ExcelSelectedFields)
            {
                IndexList.Add(GetFieldIndex(myDataTable, str.field));
            }

            return IndexList;
        }

        public static int GetPixelValue(int x, int y, IRasterLayer layer, IPixelBlock pixelBlock)
        {
            IPnt pnt = new PntClass();
            pnt.SetCoords(x, y);
            layer.Raster.Read(pnt, pixelBlock);
            object obj = pixelBlock.GetVal(0, 0, 0);
            return Convert.ToInt32(obj);            
        }
    }
}
